package com.mazha.rabbitmq.result;

import com.alibaba.fastjson.JSONObject;
import com.mazha.rabbitmq.enums.ErrorCodeEnum;
import lombok.Data;

/**
 * 响应结果
 *
 * @author leizi
 * @date 2022-05-28 16:39
 */
@Data
public class RestResult {

    private Integer code;

    private Boolean success;

    private String errMsg;

    private Object data;

    public RestResult() {
    }

    public RestResult(Integer code, Boolean success, String errMsg, Object data) {
        this.code = code;
        this.success = success;
        this.errMsg = errMsg;
        this.data = data;
    }

    /**
     * 成功结果
     *
     * @return
     */
    public static RestResult successResult() {
        return successResult(null);
    }

    /**
     * 自定义成功结果
     *
     * @param result 自定义结果
     * @return
     */
    public static RestResult successResult(Object result) {
        RestResult restResult = new RestResult();
        restResult.code = 0;
        restResult.success = true;
        restResult.data = result;
        restResult.errMsg = "";
        return restResult;
    }

    /**
     * 错误信息结果
     *
     * @param errMsg
     * @return
     */
    public static RestResult failResult(String errMsg) {
        return failResult(1, errMsg);
    }

    /**
     * 错误码与异常信息结果
     *
     * @param code   错误码
     * @param errMsg 错误信息
     * @return
     */
    public static RestResult failResult(int code, String errMsg) {
        RestResult restResult = new RestResult();
        restResult.code = code;
        restResult.errMsg = errMsg;
        restResult.success = false;
        return restResult;
    }

    /**
     * 自定义错误码结果
     *
     * @param errorCodeEnum
     * @return
     */
    public static RestResult failResult(ErrorCodeEnum errorCodeEnum) {
        RestResult restResult = new RestResult();
        restResult.code = errorCodeEnum.getCode();
        restResult.errMsg = errorCodeEnum.getMessage();
        restResult.success = false;
        return restResult;
    }
}

